<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon>
              <Search/>
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon>
              <Refresh/>
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <el-button
            v-hasPerm="['system:lpsOrderXian:add']"
            type="success"
            @click="handleOpenDialog()"
        >
          <template #icon>
            <Plus/>
          </template>
          新增
        </el-button>
        <el-button
            v-hasPerm="['system:lpsOrderXian:delete']"
            type="danger"
            :disabled="removeIds.length === 0"
            @click="handleDelete()"
        >
          <template #icon>
            <Delete/>
          </template>
          删除
        </el-button>
      </div>

      <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="pageData"
          highlight-current-row
          border
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column type="index" label="序号" width="60" align="center"/>
        <el-table-column
            key="cgdd"
            label="采购订单"
            prop="cgdd"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="poxxm"
            label="PO行项目"
            prop="poxxm"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="xsdd"
            label="销售订单"
            prop="xsdd"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="soxxm"
            label="SO行项目"
            prop="soxxm"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="wl"
            label="物料"
            prop="wl"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="ms"
            label="描述"
            prop="ms"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="thbb"
            label="图号/版本"
            prop="thbb"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="zj"
            label="子件"
            prop="zj"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="sl"
            label="数量"
            prop="sl"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="jhrq"
            label="交货日期"
            prop="jhrq"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="azszgjhdq"
            label="安装所在国家或地区"
            prop="azszgjhdq"
            min-width="180"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="carSafetyGearType"
            label="CAR_SAFETY_GEAR_TYPE"
            prop="carSafetyGearType"
            min-width="220"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="hk"
            label="HK"
            prop="hk"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="bk"
            label="BK"
            prop="bk"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="tk"
            label="TK"
            prop="tk"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="grpAlarmType"
            label="GRP_ALARM_TYPE"
            prop="grpAlarmType"
            min-width="160"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="marketCluster"
            label="MARKET_CLUSTER"
            prop="marketCluster"
            min-width="160"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="sckLmsType"
            label="SCK_LMS_TYPE"
            prop="sckLmsType"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="dxlx"
            label="电线类型"
            prop="dxlx"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="kgsl"
            label="开关数量"
            prop="kgsl"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="dxsl"
            label="电线数量"
            prop="dxsl"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="dxzd"
            label="电线长度"
            prop="dxzd"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="dxjsz"
            label="电线计算值"
            prop="dxjsz"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="bz"
            label="备注"
            prop="bz"
            width="300"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template #default="scope">
            <el-button
                v-hasPerm="['system:lpsOrderXian:edit']"
                type="primary"
                size="small"
                link
                @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon>
                <Edit/>
              </template>
              编辑
            </el-button>
            <el-button
                v-hasPerm="['system:lpsOrderXian:delete']"
                type="danger"
                size="small"
                link
                @click="handleDelete(scope.row.id)"
            >
              <template #icon>
                <Delete/>
              </template>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleQuery()"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "LpsOrderXian",
  inheritAttrs: false,
});

import LpsOrderXianAPI, {
  LpsOrderXianForm,
  LpsOrderXianPageQuery,
  LpsOrderXianPageVO
} from "@/api/system/lps-order-xian";

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<LpsOrderXianPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 新产品 线表格数据
const pageData = ref<LpsOrderXianPageVO[]>([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 新产品 线表单数据
const formData = reactive<LpsOrderXianForm>({});

// 新产品 线表单校验规则
const rules = reactive({});

/** 查询新产品 线 */
function handleQuery() {
  loading.value = true;
  LpsOrderXianAPI.getPage(queryParams)
      .then((data) => {
        pageData.value = data.list;
        total.value = data.total;
      })
      .finally(() => {
        loading.value = false;
      });
}

/** 重置新产品 线查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 打开新产品 线弹窗 */
function handleOpenDialog(id?: number) {
  dialog.visible = true;
  if (id) {
    dialog.title = "修改新产品 线";
    LpsOrderXianAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增新产品 线";
  }
}

/** 提交新产品 线表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        LpsOrderXianAPI.update(id, formData)
            .then(() => {
              ElMessage.success("修改成功");
              handleCloseDialog();
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      } else {
        LpsOrderXianAPI.add(formData)
            .then(() => {
              ElMessage.success("新增成功");
              handleCloseDialog();
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭新产品 线弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除新产品 线 */
function handleDelete(id?: number) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
      () => {
        loading.value = true;
        LpsOrderXianAPI.deleteByIds(ids)
            .then(() => {
              ElMessage.success("删除成功");
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      },
      () => {
        ElMessage.info("已取消删除");
      }
  );
}

onMounted(() => {
  handleQuery();
});
</script>
